pepperstad.ResourceLoader = function () {

    this.resources = {};

    var config = pepperstad.config;
    var self = this;

    /**
     * @return boolean true if all resources are loaded
     */
    this.loadingComplete = function() {
        var allResourcesLoaded = true;
        $.each(this.resources, function(index, value) {
            if (value === null) {
                allResourcesLoaded = false;
                return false;
            }
        });
        return allResourcesLoaded;
    };
    this.loadResources = function() {
        load("story", "res/stories/" + config.story + "/story.json");
        load("story.config", "res/stories/" + config.story + "/config.json");
        load("story.structures", "res/stories/" + config.story + "/structure-data.json");
        load("skills", "res/skills.json");
        load("occupations", "res/occupations.json");
        load("names", "res/names.json");
        load("surnames", "res/surnames.json");
        load("weapons", "res/weapons.json");
        load("armor", "res/armor.json");
        load("equipment", "res/equipment.json");
        load("lang", "lang/" + config.lang + ".json");
        load("buildings", "res/buildings.json");
        load("structureState", "res/structure-state.json");
    };
    function load(resourceName, url) {
        self.resources[resourceName] = null;
        $.getJSON(config.rootPath + "/" + url +
                (config.cacheResourceFiles
                    ? ""
                    : "?nocache=" + (new Date()).getTime()),
            function resloader(data) {
                self.resources[resourceName] = data;
                log("resource loaded: " + resourceName);
            }).error(function(xx) {
                log("failed to load " + resourceName + "." + xx);
            });
    }

    this.get = function(resourceName) {
        return this.resources[resourceName];
    };
};
